<script setup lang="ts">
defineOptions({name:'PatientPage'})
import {getPatientListAPI,addPatientAPI,editPatientAPI,delPatientAPI} from '@/services/user'
import type{PatientListType,PatientType} from '@/types/user'
import {onMounted, ref} from 'vue'
import {nameRules,idCardRules} from '@/utils/rules'
import { showConfirmDialog,showSuccessToast, type FormInstance } from 'vant';
import { computed } from 'vue' ;

//创建响应式数据保存患者列表数据
const patientList = ref<PatientListType>([])

//控制侧滑层
const show = ref(false)

//性别选择
const options = [
  { label: '男', value: 1 },
  { label: '女', value: 0 }
]

// 打开弹层清空表单
const ininPatient:PatientType = {
  name: '',
  idCard: '',
  gender: 1,
  defaultFlag: 0
}

//表单数据
const patient = ref<PatientType>({
  ...ininPatient
})

// 默认值需要转换
// set()  设置   修改时执行
// get()  获取   使用时执行
//因为勾选默认的时候Vue拿到的是true或false，接口要的是0和1，所以需要转换
const defaultFlag = computed({
  set(value) {
    patient.value.defaultFlag = value ? 1 : 0
  },
  get() {
    return patient.value.defaultFlag === 1 ? true : false
  }

})
//获取患者列表
const getPatientList = async() => {
 const res = await getPatientListAPI()
 console.log(res);
 patientList.value = res.data
}

onMounted(()=>{
    getPatientList()
})

const formRef = ref<FormInstance>()

//点击添加患者
const showPopup = (item?:PatientType) =>{
if(item){
  //有item点击的是编辑
  //解构对象取数据
  const { id,name,idCard,defaultFlag,gender } = item
  patient.value = { id,name,idCard,defaultFlag,gender }
}else{
  //添加功能
  patient.value = {...ininPatient}
}
show.value = true
}
//点击保存校验表单信息
const onSubmit = async() => {
  //对表单进行预校验
  await formRef.value?.validate()
   // 身份证倒数第二位，单数是男，双数是女
   const gender = Number(patient.value.idCard?.slice(-2, -1)) % 2
   if(gender !== patient.value.gender){
    await showConfirmDialog({
      title: '温馨提示',
      message: '您填写的性别和身份证号中的不一致'
    })
   }
   //添加或修改
   patient.value.id ? await editPatientAPI(patient.value) :  await addPatientAPI(patient.value)
   show.value = false
   getPatientList()
   showSuccessToast(patient.value.id ? '编辑成功' : '添加成功')
}

//删除患者
// 1.准备按钮
//2.封装,调用删除api方法
//3.点击删除，调用删除api方法
const remove = async() =>{
  if(patient.value.id){
    await showConfirmDialog({
      title:'温馨提示',
      message:'您确认要删除患者信息吗?'
    })
    await delPatientAPI(patient.value.id)
    getPatientList()
    showSuccessToast('删除成功')
    show.value = false
  }
}
</script>

<template>
    <div class="patient-page">
      <cp-nav-bar title="家庭档案"></cp-nav-bar>
      <div class="patient-list">
        <div class="patient-item"  v-for="item in patientList" :key="item.id">
          <div class="info">
            <span class="name">{{ item.name }}</span>
            <span class="id">{{ item.idCard?.replace(/^(\w{6})(\w{8})(\w{4})$/, '\$1********\$3') }}</span>
            <span>{{ item.genderValue }}</span>
            <span>{{ item.age }}岁</span>
          </div>
          <div class="icon"><cp-icon name="user-edit"  @click="showPopup(item)"/></div>
          <div class="tag" v-if="item.defaultFlag === 1">默认</div>
        </div>
        <div class="patient-add" v-if="patientList.length < 6" @click="showPopup()">
          <cp-icon name="user-add"/>
          <p>添加患者</p>
        </div>
        <div class="patient-tip">最多可添加 6 人</div>
      </div>
      <!-- 侧滑层 -->
    <van-popup v-model:show="show" position="right" >
      <cp-nav-bar 
      :title="patient.id ? '编辑患者' : '添加患者'" 
      right-text="保存" 
      :back="()=> (show = false)" 
      @click-right="onSubmit">
    </cp-nav-bar>
      <van-form ref="formRef" autocomplete="off">
        <van-field
          label="真实姓名："
          placeholder="用请输入真实姓名"
          v-model="patient.name"
          :rules="nameRules"
        />
        <van-field
          label="身份证号："
          placeholder="请输入身份证号"
          v-model="patient.idCard"
          :rules="idCardRules"
        />
        <van-field label="性别"  class="pb4">
          <!-- 单选按钮组件 -->
          <template #input>
            <cp-radio-btn :options="options" v-model="patient.gender"></cp-radio-btn>
          </template>
        </van-field>
        <van-field label="默认就诊人">
          <template #input>
            <van-checkbox :icon-size="18" round  v-model="defaultFlag"/>
          </template>
        </van-field>
        <van-action-bar v-if="patient.id">
          <van-action-bar-button  text="删除患者"  @click="remove"/>
        </van-action-bar>
    </van-form>
    </van-popup>
    </div>
  </template>

<style scoped lang="scss">
.patient-page {
  padding: 46px 0 80px;
  :deep() {
    .van-popup {
      width: 100%;
      height: 100%;
      padding-top: 46px;
      box-sizing: border-box;
    }
  }
}
.patient-list {
  padding: 15px;
}
.patient-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: var(--cp-bg);
  border-radius: 8px;
  margin-bottom: 15px;
  position: relative;
  border: 1px solid var(--cp-bg);
  transition: all 0.3s;
  overflow: hidden;
  .info {
    display: flex;
    flex-wrap: wrap;
    flex: 1;
    span {
      color: var(--cp-tip);
      margin-right: 20px;
      line-height: 30px;
      &.name {
        font-size: 16px;
        color: var(--cp-text1);
        width: 80px;
        margin-right: 0;
      }
      &.id {
        color: var(--cp-text2);
        width: 180px;
      }
    }
  }
  .icon {
    color: var(--cp-tag);
    width: 20px;
    text-align: center;
  }
  .tag {
    position: absolute;
    right: 60px;
    top: 21px;
    width: 30px;
    height: 16px;
    font-size: 10px;
    color: #fff;
    background-color: var(--cp-primary);
    border-radius: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  &.selected {
    border-color: var(--cp-primary);
    background-color: var(--cp-plain);
    .icon {
      color: var(--cp-primary);
    }
  }
}
.patient-add {
  background-color: var(--cp-bg);
  color: var(--cp-primary);
  text-align: center;
  padding: 15px 0;
  border-radius: 8px;
  .cp-icon {
    font-size: 24px;
  }
}
.patient-tip {
  color: var(--cp-tag);
  padding: 12px 0;
}
.pb4 {
  padding-bottom: 4px;
}
// 底部操作栏
.van-action-bar {
  padding: 0 10px;
  margin-bottom: 10px;
  .van-button {
    color: var(--cp-price);
    background-color: var(--cp-bg);
  }
}
</style>